/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "common.h"
#include "ia_stats_model.h"

IAStatsModel::IAStatsModel(QObject * parent)
    : QAbstractTableModel(parent)
{
}

int IAStatsModel::rowCount(const QModelIndex & parent) const
{
    return dirNames.size();
}

int IAStatsModel::columnCount(const QModelIndex & parent) const
{
    return 2;
}

QVariant IAStatsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal) {
        switch (section) {
            case 0:
                return QString(tr("Session"));
            case 1:
                return QString(tr("Month"));
            default:
                return QVariant();
        };
    } else {
        if (section > dirNames.size())
            return QVariant();
        return dirNames.at(section);
    }
}

QVariant IAStatsModel::data(const QModelIndex & index, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (index.row() > dirNames.size())
        return QVariant();

    int idx = dirIndex[dirNames[index.row()]];

    switch (index.column()) {
        case 0:
            return QString("%1 / %2").arg(IntToKMG(stat.su[idx], units)).arg(IntToKMG(stat.sd[idx], units));
        case 1:
            return QString("%1 / %2").arg(IntToKMG(stat.mu[idx], units)).arg(IntToKMG(stat.md[idx], units));
        default:
            return QVariant();
    };

    return QVariant();
}

void IAStatsModel::SetStats(const LOADSTAT & s)
{
    stat = s;
    reset();
}

void IAStatsModel::SetDirNames(const QStringList & dn,
                               const QMap<QString, int> & di)
{
    dirNames = dn;
    dirIndex = di;
    reset();
}

QString IAStatsModel::getFreeMb() const
{
    return QString(reinterpret_cast<const char *>(&stat.freeMb[0]));
}

QString IAStatsModel::getCash() const
{
    return QString::number(stat.cash / 1000.0, 'f', 2);
}
